app.controller("loginController",function($scope,$http,$location){
    //登陆方法验证
    $scope.login=function(){
        $http({
            method:"post",
            url:"/zzlp/login",
            data:{"name":$scope.jobId,"password":$scope.pwd}

        }).then(function successCallBack(response){

            console.log(response.data);
            var user=response.data;
            if(user==null||user==""){
                console.log('3');
                $scope.invalid=true;
            }else{
                console.log("4");
                $scope.invalid=false;
                //alert("登陆成功!");
                console.log("5");

                window.location="/zzlp/html/home.html";

            }
        });
    }
  //  $scope.Data=socket.collection;

});

app.controller("chatController",function($scope,$rootScope,MyData){
    $scope.MyData=MyData;
    console.log($scope.MyData)
    alert("aaaaa "+ $scope.MyData.collection);

});



// app.factory('socket', function() {
//     var service = {};
//     service.ws = {};
//     var collection = [];
//
//
//     service.connect = function() {
//         this.ws = new WebSocket("ws://localhost:8080/zzlp/socketServer");
//         this.ws.onmessage = function(msg) {
//             alert(msg.data);
//             try {
//                 collection.push(msg.data);
//                // alert('roba: '+JSON.parse(msg.data).lat+' :: '+JSON.parse(msg.data).lng);
//             } catch(err) {
//                 console.log('err.message', err.message);
//                 alert(err.message);
//             }
//         };
//         this.ws.onerror = function(evt) {
//             alert('error: '+evt.data);
//         };
//     };
//     service.disconnect = function() {
//         if(this.ws != undefined && this.ws != null) {
//             this.ws.onclose();
//         }
//     };
//     return service;
// });


app.factory('MyData',function ($rootScope) {
    var dataStream=new WebSocket('ws://localhost:8080/zzlp/socketServer');
    var collection=[];

    dataStream.onmessage = function(msg) {
        collection.push(msg.data);
        $rootScope.$apply();
            }

    var methods={
        collection:collection,
        get:function () {
            dataStream.send(JSON.stringify({action:'get'}));
        }

    };
    return methods;
});



